package com.sun.showcase.controller.basic;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sun.showcase.biz.basic.service.GroupUserService;
import com.sun.showcase.client.domain.basic.GroupUser;
import com.sun.showcase.client.query.basic.GroupUserQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Result;
import com.sun.showcase.utils.LoginContextHolder;

@Controller
@RequestMapping(value="/basic/groupUser") 
public class GroupUserController{
	
	@Autowired
	private GroupUserService groupUserService;
	
	private Result result = new Result();
	/**
	 * 跳转到GroupUser管理页面
	 * 
	 * @return
	 */
	@RequestMapping(value="goGroupUser")
	public String goGroupUser(@RequestParam("groupId") String groupId , ModelMap map) {
		GroupUserQuery groupUserQuery = new GroupUserQuery();
		groupUserQuery.setUserId(LoginContextHolder.get().getUserId());
		groupUserQuery.setGroupId(groupId);
		List<GroupUserQuery> groupUserList = groupUserService.listAll(groupUserQuery);
		
		if(groupUserList!=null&&groupUserList.size()>0)
			BeanUtils.copyProperties(groupUserList.get(0), groupUserQuery);
		 	map.addAttribute("groupId", groupId);
		 	map.addAttribute("isAdmin", groupUserQuery.getIsAdmin());
		return "/basic/groupUser";
	}
	/**
	 * 跳转到GroupUser新增页面
	 * 
	 * @return
	 */
	@RequestMapping(value="handleGroupUser")
	public String handleGroupUser(@RequestParam(value="id",required=false) Long id ,ModelMap map) {
		return "basic/groupUser_rowEdit";
	}

	/**
	 * 跳转到查看desc页面
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="showDesc")
	public GroupUser showDesc(GroupUserQuery groupUserQuery) {
		return groupUserService.get(groupUserQuery);
	}

	/**
	 * 获得分页数据表格
	 */
	@ResponseBody
	@RequestMapping(value = "/datagrid")
	public DataGrid datagrid(GroupUserQuery groupUserQuery) {
		return groupUserService.datagrid(groupUserQuery);
	}
	

	/**
	 * 添加一个GroupUser
	 */
	@ResponseBody
	@RequestMapping(value="add")
	public Result add(GroupUserQuery groupUserQuery) {
		GroupUser groupUser = groupUserService.getByUserAndGroup(groupUserQuery);
		if(groupUser!=null){
			result.setMsg("该用户已存在组内！");
			return result;
		}
		groupUserService.add(groupUserQuery);
		result.setSuccess(true);
		result.setData(groupUserQuery);
		result.setMsg("添加成功！");
		return result;
	}

	/**
	 * 编辑GroupUser
	 */
	@ResponseBody
	@RequestMapping(value="edit")
	public Result edit(@RequestBody GroupUserQuery groupUserQuery) {
		groupUserService.update(groupUserQuery);
		result.setSuccess(true);
		result.setMsg("编辑成功！");
		return result;
	}

	/**
	 * 物理删除GroupUser
	 */
	@ResponseBody
	@RequestMapping(value="delete")
	public Result delete(@RequestBody GroupUserQuery groupUserQuery) {
		groupUserService.delete(groupUserQuery);
		result.setSuccess(true);
		result.setMsg("删除成功！");
		return result;
	}
	
	/**
	 * 逻辑删除GroupUser
	 */
	@ResponseBody
	@RequestMapping(value="deletePt")
	public Result deletePt(GroupUserQuery groupUserQuery) {
		groupUserService.deletePt(groupUserQuery.getIds());
		result.setSuccess(true);
		result.setMsg("删除成功！");
		return result;
	}
}
